<template>
  <div class="echart-panel">
    <div class="head">
      <div class="title">
        <div
        class="iconfont"
        :class="icon"
        :style="{
          'background-color': iconBgColor,
          'color': iconColor
        }"></div>
        <div>{{title}}</div>
      </div>
      <slot></slot>
    </div>
    <div class="content">
      <mpvue-echarts
        :echarts="echarts"
        :onInit="initEchart"
        :canvasId="canvasId" />
    </div>
  </div>
</template>

<script>
// import echarts from 'echarts'
import echarts from '../../../static/js/echarts.min.js'
import mpvueEcharts from 'mpvue-echarts'

export default {
  components: {
    mpvueEcharts
  },
  props: {
    icon: {
      type: String,
      default: ''
    },
    iconColor: {
      type: String,
      default: 'white'
    },
    iconBgColor: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    echartObj: {
      type: Object,
      default: () => ({})
    },
    canvasId: {
      type: String,
      default: ''
    }
  },
  watch: {
    echartObj () {
      this.echartSetOption()
    }
  },
  data () {
    return {
      echarts
    }
  },
  methods: {
    echartSetOption () {
      if (this.echart && JSON.stringify(this.echartObj) !== '{}') {
        this.echart.setOption(this.echartObj)
      }
    },
    initEchart (canvas, width, height) {
      console.log(echarts)
      this.echart = echarts.init(canvas, null, {
        width: width,
        height: height
      })
      canvas.setChart(this.echart)

      this.echartSetOption()

      return this.echart
    }
  }
}
</script>

<style lang="scss" scoped>
  .echart-panel {
    width: 100%;
    height: 250px;
    background-color: white;
    overflow: hidden;
    position: relative;
    > .head {
      width: 100%;
      height: 40px;
      padding: 0 10px;
      display: flex;
      align-items: center;
      box-sizing: border-box;
      border-bottom: 1px solid rgb(242, 242, 242);
      > .title {
        flex: 1;
        height: 100%;
        font-size: 16px;
        display: flex;
        align-items: center;
        > .iconfont {
          width: 20px;
          height: 20px;
          border-radius: 50%;
          margin-right: 5px;
          text-align: center;
          line-height: 20px;
        }
      }
    }
    > .content {
      width: 100%;
      height: calc(100% - 40px);
      position: relative;
      overflow: hidden;
      > .echart {
        width: 100%;
        height: 100%;
        position: relative;
        overflow: hidden;
      }
    }
  }
</style>
